﻿
@{
    ViewData["Title"] = "AddEditSellReturnIndex";
    Layout = "~/Views/Share/_LayoutBody.cshtml";
}

<style>
    #bottomField {
        position: fixed;
        top: 0;
        right: 25px;
        background-color: rgba(0, 0, 0, 0.1);
        border-radius: 0px 0px 6px 6px;
        padding: 10px;
    }

        #bottomField .btn-group {
            font-size: 14px;
            margin-top: -1px;
            vertical-align: middle;
            color: #666666;
        }

    .layui-form-label {
        width: 80px;
        padding: 2px 10px 0px 0px;
    }

    .ui-jqgrid .ui-jqgrid-btable tbody tr.jqgrow td {
        padding-top: 5px;
        padding-bottom: 5px;
        line-height: 20px;
    }

    .modal-content {
        position: absolute;
    }
</style>
<div class="layui-fluid">
    <div class="layui-row layui-col-space10">
        <div class="layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-body" style=" height:500px; margin-bottom:20px;box-shadow: 0 0 3px rgba(0, 0, 0, 0.5);">
                    <form class="layui-form">
                        <div class="layui-form-item" style="margin-bottom:5px">
                            <label class="layui-form-label">单据日期</label>
                            <div class="layui-input-inline" style="width:140px">
                                <input type="text" name="BillDate" class="layui-input" id="BillDate">
                            </div>
                            <label class="layui-form-label">销售员</label>
                            <div class="layui-input-inline" style="width:140px">
                                <select id="SellName" name="SellName" lay-search lay-verify="required">
                                    <option value="">==请选择==</option>
                                </select>
                            </div>
                            <label class="layui-form-label">仓库</label>
                            <div class="layui-input-inline" style="width:140px">
                                <select id="StoreName" name="StoreName" lay-search lay-verify="required" lay-filter="StoreName">
                                    <option value="">==请选择==</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">客户</label>
                            <div class="layui-input-inline" style="width:140px">
                                <select id="SupName" name="SupName" lay-search lay-verify="required">
                                    <option value="">==请选择==</option>
                                </select>
                            </div>
                            <label class="layui-form-label">快递费</label>
                            <div class="layui-input-inline" style="width:140px">
                                <input type="number" name="GiveSum" class="layui-input" id="GiveSum">
                            </div>
                            <label class="layui-form-label">总金额</label>
                            <div class="layui-input-inline" style="width:140px">
                                <input type="text" name="Sum" class="layui-input" id="Sum" readonly="true">
                            </div>
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-inline" style="width:140px">
                                <input type="text" id="BZ" name="BZ" class="layui-input" style="width:140px;">
                            </div>
                        </div>
                    </form>

                    <table id="list1" class="table table-condensed"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="bottomField">
    <button type="button" class="layui-btn layui-btn-sm" data-type="Choose" id="Choose"><i class="layui-icon">&#xe65c;</i>选择销售单</button>
    <button type="button" class="layui-btn layui-btn-sm" data-type="Empty" id="Empty"><i class="layui-icon">&#xe6b1;</i>清空</button>
    <button type="button" class="layui-btn layui-btn-sm" data-type="SaveAdd" id="SaveAdd"><i class="layui-icon">&#xe61f;</i>保存并新增</button>
    <button type="button" class="layui-btn layui-btn-sm" data-type="AddClose" id="AddClose"><i class="layui-icon ">&#xe608;</i>保存</button>
    <button type="button" class="layui-btn layui-btn-sm" data-type="AddSH" id="AddSH"><i class="layui-icon ">&#xe672;</i>保存并过账</button>
</div>
<script>
    var keyValuetypes = $.request("types");//判断是新增或修改
    var keyValueids = $.request("ids");//修改的单据号
    var lastrow;  //最后修改行号先定义一个全局变量
    var lastcell; //最后修改列号先定义一个全局变量
    var layueform;
    //初始化界面
    $(function () {
        var bodyheight = document.body.scrollHeight;
        $(".layui-card-body").css("height", bodyheight - 55);

        $("#list1").jqGrid({
            styleUI: 'Bootstrap',
            datatype: "local",
            jsonReader: {
                repeatitems: false
            },
            colModel: [
                { label: 'ids', name: 'ids', index: 'ids', key: true, width: 75, hidden: true },
                {
                    label: '操作', name: 'Hand', index: 'Hand', width: 100, align: 'center', formatter: function (value, grid, rows, state) {
                        var detail = ' <button onclick="AddGoods(\'' + rows.ids + '\');" class="layui-btn layui-btn-xs" lay-event="AddsGoods">新增</button> <button onclick="DelRow(\'' + rows.ids + '\');" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="DelRows">删除</button>';
                        return detail;
                    }
                },
                { label: '主键id', name: 'id', index: 'id', width: 100, align: 'center', hidden: true },
                { label: '库存行id', name: 'StoreRow', index: 'StoreRow', width: 100, align: 'center', hidden: true },
                { label: '商品id', name: 'Good_id', index: 'Good_id', width: 100, align: 'center', hidden: true },
                { label: '商品编号', name: 'GoodID', index: 'GoodID', width: 100, align: 'center'},
                { label: '*商品名称', name: 'GoodName', index: 'GoodName', width: 140, align: 'center' },
                { label: '单位', name: 'DW', index: 'DW', width: 80, align: 'center' },
                { label: '规格', name: 'GGType', index: 'GGType', width: 100, align: 'center' },
                {
                    label: '*数量', name: 'Num', index: 'Num', width: 100, align: 'center', editable: true,
                    editrules: {
                        edithidden: true, number: true, minValue: 0
                    }

                },
                {
                    label: '*退款单价', name: 'SellPrice', index: 'SellPrice', width: 100, align: 'center', editable: true,
                    editrules: {
                        edithidden: true, number: true, minValue: 0
                    }

                },
                { label: '退款金额', name: 'Sum', index: 'Sum', width: 100, align: 'center' },
                { label: '生产批号', name: 'SCPH', index: 'SCPH', width: 100, align: 'center' },
                { label: '灭菌批号', name: 'MJPH', index: 'MJPH', width: 100, align: 'center'},
                { label: '生产日期', name: 'scDate', index: 'scDate', width: 100, align: 'center', },
                {label: '有效期至', name: 'yxqDate', index: 'yxqDate', width: 100, align: 'center',},
                { label: '进货单价', name: 'InPrice', index: 'InPrice', width: 100, align: 'center', },
                { label: '型号', name: 'ModelType', index: 'ModelType', width: 100, align: 'center' },
                { label: '生产厂家', name: 'SCCJ', index: 'SCCJ', width: 100, align: 'center' },
                { label: '备注', name: 'BZ', index: 'BZ', width: 140, align: 'center', editable: true, },

            ],
            width: "100%",
            autowidth: true,
            height: bodyheight-180,
            sortname: 'ID',//初始化的时候排序的字段
            sortorder: "desc",//排序方式,可选desc,asc
            //mtype: "post",//向后台请求数据的ajax的类型。可选post,get
            viewrecords: true,
            shrinkToFit: false,
            rownumbers: true,
            cellEdit: true,
            cellsubmit: 'clientArray',
            beforeEditCell: function (rowid, cellname, v, iRow, iCol) {
                lastrow = iRow;  //给全局变量赋值
                lastcell = iCol;
            }

        });
        //填充100行空行
        for (var i = 1; i <= 100; i++) {
            var item = {
                "ids": i,
                "Good_id": '',
                "GoodID": '',
                "GoodName": '',
                "DW": '',
                "GGType": '',
                "ModelType": '',
                "SCCJ": '',
                "InPrice": '',
                "SellPrice": '',
                "Num": '',
                "Sum": '',
                "BZ": ''
            };
            $("#list1").jqGrid("addRowData", i, item, "last");
        }
        layui.use(['table', 'form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate
                , table = layui.table;

            var strdate = new Date().Format("yyyy-MM-dd");
            //销售员
            $("#SellName").bindSelect({
                url: "/SellReturn/GetUser",
                id: "id",
                text: "UserName"
            });
            //客商
            $("#SupName").bindSelect({
                url: "/SellReturn/GetSup",
                id: "id",
                text: "SupName"
            });

            //仓库
            $("#StoreName").bindSelect({
                url: "/SellReturn/GetStoreInfo",
                id: "id",
                text: "StoreName"
            });
            //新增初始化
            if (keyValuetypes == "add") {
                var strdate = new Date().Format("yyyy-MM-dd");
                laydate.render({
                    elem: '#BillDate',//制定元素
                    type: 'date',
                    show: false,//默认显示
                    position: 'fixed',
                    value: strdate,
                    isInitValue: true, //是否允许填充初始值，默认为 true

                });
            }
            //修改初始化
            else if (keyValuetypes == "edit") {
                if (!!keyValueids) {
                    $.ajax({
                        url: "/SellReturn/GetBillList",
                        data: { ids: keyValueids },
                        dataType: "json",
                        async: false,
                        success: function (datas) {
                            var sellbill = datas.bills;
                            var sellbill_mx = datas.bills.sellBill_MX;
                            $("#SellName").val(sellbill.sellNameID);
                            $("#SupName").val(sellbill.sup_id);
                            var BillDates = sellbill.billDate.toString().substring(0, 10);
                            $("#BillDate").val(BillDates);
                            $("#StoreName").val(sellbill.stroeInfo_id);
                            $("#Sum").val(sellbill.sum);
                            $("#GiveSum").val(sellbill.giveSum);
                            $("#BZ").val(sellbill.bz);
                            $.each(sellbill_mx, function (i) {
                                var scdates = "";
                                var yxqDate = "";
                                if (sellbill_mx[i].scDate != null && sellbill_mx[i].scDate !=undefined) {
                                    scdates = sellbill_mx[i].scDate.toString().substring(0, 10);
                                }
                                if (sellbill_mx[i].yxqDate != null && sellbill_mx[i].yxqDate != undefined) {
                                    yxqDate = sellbill_mx[i].yxqDate.toString().substring(0, 10);
                                }
                                var item = {
                                    "id": sellbill_mx[i].id,
                                    "StoreRow": sellbill_mx[i].storeRow,
                                    "Good_id": sellbill_mx[i].good_id,
                                    "GoodID": sellbill_mx[i].goodID,
                                    "GoodName": sellbill_mx[i].goodName,
                                    "DW": sellbill_mx[i].dw,
                                    "GGType": sellbill_mx[i].ggType,
                                    "ModelType": sellbill_mx[i].modelType,
                                    "SCCJ": sellbill_mx[i].sccj,
                                    "InPrice": sellbill_mx[i].inPrice,
                                    "SellPrice": sellbill_mx[i].sellPrice,
                                    "Num": sellbill_mx[i].num,
                                    "Sum": sellbill_mx[i].sum,
                                    "SCPH": sellbill_mx[i].scph,
                                    "MJPH": sellbill_mx[i].mjph,
                                    "scDate": scdates,
                                    "yxqDate": yxqDate,
                                    "BZ": sellbill_mx[i].bz,
                                };
                                AddRows(i + 1, item);

                            });


                        }
                    });
                    $('#Empty').hide();
                    $('#SaveAdd').hide();
                }
                laydate.render({
                    elem: '#BillDate',//制定元素
                    type: 'date',
                    show: false,//默认显示
                    position: 'fixed',
                    //value: strdate,
                    //isInitValue: true, //是否允许填充初始值，默认为 true

                });

            }
            form.on('select(StoreName)', function (data) {
                for (var i = 1; i <= 100; i++) {
                    DelRow(i);
                }
                $("#Sum").val('');
                $("#GiveSum").val('');
            });
            layueform = form;
            form.render();

        });

    });
    //选择商品弹出框
    function AddGoods(ids) {
        var StroeInfo_id = $('#StoreName').val();
        if (StroeInfo_id == "" || StroeInfo_id == undefined || StroeInfo_id == null) {
            layer.msg("请选择仓库", { icon: 2 });
            return false;
        }
        var url = "/SellReturn/AddRowIndex" + "?ids=" + ids +"&StroeInfo_id=" + StroeInfo_id;
        $.FromOpen('SellFromAddRow', '选择商品库存信息', url, '600px', '500px', '1000');
    };

    //新增一行商品库存
    function GoodsAddRows(ids, item) {
        $("#list1").jqGrid('setCell', ids, 'StoreRow', item.id);
        $("#list1").jqGrid('setCell', ids, 'Good_id', item.Good_id);
        $("#list1").jqGrid('setCell', ids, 'GoodID', item.GoodID);
        $("#list1").jqGrid('setCell', ids, 'GoodName', item.GoodName);
        $("#list1").jqGrid('setCell', ids, 'DW', item.DW);
        $("#list1").jqGrid('setCell', ids, 'GGType', item.GGType);
        $("#list1").jqGrid('setCell', ids, 'ModelType', item.ModelType);
        $("#list1").jqGrid('setCell', ids, 'SCCJ', item.SCCJ);
        $("#list1").jqGrid('setCell', ids, 'InPrice', item.InPrice);
        $("#list1").jqGrid('setCell', ids, 'SellPrice', item.SellPrice);
        $("#list1").jqGrid('setCell', ids, 'Num', 1);
        $("#list1").jqGrid('setCell', ids, 'Sum', item.SellPrice);
        $("#list1").jqGrid('setCell', ids, 'SCPH', item.SCPH);
        $("#list1").jqGrid('setCell', ids, 'MJPH', item.MJPH);
        $("#list1").jqGrid('setCell', ids, 'scDate', item.scDate);
        $("#list1").jqGrid('setCell', ids, 'yxqDate', item.yxqDate);
        $("#list1").jqGrid('setCell', ids, 'BZ', item.BZ);
        SumMoney();
    };
    //加载出库单
    function AddRows(ids, item) {
        //$("#list1").jqGrid('setCell', ids, 'id', item.id);
        $("#list1").jqGrid('setCell', ids, 'StoreRow', item.StoreRow);
        $("#list1").jqGrid('setCell', ids, 'Good_id', item.Good_id);
        $("#list1").jqGrid('setCell', ids, 'GoodID', item.GoodID);
        $("#list1").jqGrid('setCell', ids, 'GoodName', item.GoodName);
        $("#list1").jqGrid('setCell', ids, 'DW', item.DW);
        $("#list1").jqGrid('setCell', ids, 'GGType', item.GGType);
        $("#list1").jqGrid('setCell', ids, 'ModelType', item.ModelType);
        $("#list1").jqGrid('setCell', ids, 'SCCJ', item.SCCJ);
        $("#list1").jqGrid('setCell', ids, 'InPrice', item.InPrice);
        $("#list1").jqGrid('setCell', ids, 'SellPrice', item.SellPrice);
        $("#list1").jqGrid('setCell', ids, 'Num', item.Num);
        $("#list1").jqGrid('setCell', ids, 'Sum', item.Sum);
        $("#list1").jqGrid('setCell', ids, 'SCPH', item.SCPH);
        $("#list1").jqGrid('setCell', ids, 'MJPH', item.MJPH);
        $("#list1").jqGrid('setCell', ids, 'scDate', item.scDate);
        $("#list1").jqGrid('setCell', ids, 'yxqDate', item.yxqDate);
        $("#list1").jqGrid('setCell', ids, 'BZ', item.BZ);
    };
    //删除一行商品信息
    function DelRow(ids) {
        $("#list1").jqGrid('setCell', ids, 'StoreRow', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'Good_id', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'GoodID', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'GoodName', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'DW', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'GGType', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'ModelType', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'SCCJ', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'InPrice', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'SellPrice', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'SCPH', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'MJPH', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'scDate', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'yxqDate', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'Num', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'Sum', "&nbsp");
        $("#list1").jqGrid('setCell', ids, 'BZ', "&nbsp");
    };
    //读取销售开单信息
    function ChooseBill(ids) {
        $.ajax({
            url: "/SellReturn/GetBillList",
            data: { ids: ids },
            dataType: "json",
            async: false,
            success: function (datas) {
                var sellbill = datas.bills;
                var sellbill_mx = datas.bills.sellBill_MX;
                $("#SellName").val(sellbill.sellNameID);
                $("#SupName").val(sellbill.sup_id);
                var BillDates = sellbill.billDate.toString().substring(0, 10);
                $("#BillDate").val(BillDates);
                $("#StoreName").val(sellbill.stroeInfo_id);
                $("#Sum").val(sellbill.sum);
                $("#GiveSum").val(sellbill.giveSum);
                $("#BZ").val(sellbill.bz);
                $.each(sellbill_mx, function (i) {
                    var scdates = "";
                    var yxqDate = "";
                    if (sellbill_mx[i].scDate != null && sellbill_mx[i].scDate != undefined) {
                        scdates = sellbill_mx[i].scDate.toString().substring(0, 10);
                    }
                    if (sellbill_mx[i].yxqDate != null && sellbill_mx[i].yxqDate != undefined) {
                        yxqDate = sellbill_mx[i].yxqDate.toString().substring(0, 10);
                    }
                    var item = {
                        "id": sellbill_mx[i].id,
                        "StoreRow": sellbill_mx[i].storeRow,
                        "Good_id": sellbill_mx[i].good_id,
                        "GoodID": sellbill_mx[i].goodID,
                        "GoodName": sellbill_mx[i].goodName,
                        "DW": sellbill_mx[i].dw,
                        "GGType": sellbill_mx[i].ggType,
                        "ModelType": sellbill_mx[i].modelType,
                        "SCCJ": sellbill_mx[i].sccj,
                        "InPrice": sellbill_mx[i].inPrice,
                        "SellPrice": sellbill_mx[i].sellPrice,
                        "Num": sellbill_mx[i].num,
                        "Sum": sellbill_mx[i].sum,
                        "SCPH": sellbill_mx[i].scph,
                        "MJPH": sellbill_mx[i].mjph,
                        "scDate": scdates,
                        "yxqDate": yxqDate,
                        "BZ": sellbill_mx[i].bz,
                    };
                    AddRows(i + 1, item);

                });


            }
        });
        layueform.render();
    };
    $(function () {
        $.hands = {
            //清空
            Empty: function () {
                for (var i = 1; i <= 100; i++) {
                    DelRow(i);
                }
                $("#SupName").val('');
                $("#Sum").val('');
                $("#GiveSum").val('');
                $("#SellName").val('');
                $("#StoreName").val('');
                layueform.render('select');
            },
            //保存并新增
            SaveAdd: function () {
                var shs = "no";
                var flag = $.hands.Add(keyValuetypes, shs);
                if (flag) {
                    var rowdata = [];
                    for (var i = 1; i <= 100; i++) {
                        DelRow(i);
                    }
                };

            },
            //保存并关闭
            AddClose: function () {
                var shs = "no";
                var flag = $.hands.Add(keyValuetypes, shs);
                if (flag) {
                    top.$("#LAY_app_tabsheader").children(".layui-this").children(".layui-tab-close").trigger("click");
                };

            },
            //保存并审核
            AddSH: function () {
                var shs = "yes";
                var flag = $.hands.Add(keyValuetypes,shs);
                if (flag) {
                    top.$("#LAY_app_tabsheader").children(".layui-this").children(".layui-tab-close").trigger("click");
                };

            },
            //保存
            Add: function (BillType,SHType) {
                $('#list1').jqGrid("saveCell", lastrow, lastcell);
                //选择日期判断
                var BillDate = $('#BillDate').val();
                if (BillDate == "" || BillDate == undefined || BillDate == null) {
                    layer.msg("请选择日期", { icon: 2 });
                    return false;
                }
                //选择销售员判断
                var SellNameID = $('#SellName').val();
                if (SellNameID == "" || SellNameID == undefined || SellNameID == null) {
                    layer.msg("请选择销售员", { icon: 2 });
                    return false;
                }
                var SellName = $("#SellName option:selected").text();
                //选择销售员判断
                var StroeInfo_id = $('#StoreName').val();
                if (StroeInfo_id == "" || StroeInfo_id == undefined || StroeInfo_id == null) {
                    layer.msg("请选择仓库", { icon: 2 });
                    return false;
                }
                var StoreName = $("#StoreName option:selected").text();

                   //选择供应商判断
                var Sup_id = $('#SupName').val();
                if (Sup_id == "" || Sup_id == undefined || Sup_id == null) {
                    layer.msg("请选择客户", { icon: 2 });
                    return false;
                }
                var SupName = $("#SupName option:selected").text();


                var BZ = $('#BZ').val();
                //明细数据
                var AddData = [];
                var datas = $("#list1").jqGrid("getRowData");
                //判断明细是否是否正确
                var dataFlag = true;
               // 求和
                var sums = 0;
                $.each(datas, function (i) {
                    var row = datas[i];
                    if (row.GoodName != "" && row.GoodName != undefined && row.GoodName != null) {
                        var rowsum = 0;
                        //判断单价数量是否为空或者数量为0
                        var datanum = Number(row.Num);
                        var dataPrice = Number(row.SellPrice);
                        var dataSum = Number(row.Sum);
                        if (isNaN(datanum)|| isNaN(dataPrice)) {
                            dataFlag = false;
                            return false;
                        }
                        if (datanum == 0) {
                            dataFlag = false;
                            return false;
                        }
                        //rowsum = (Math.round(dataPrice * 100) * datanum) / 100;
                        rowsum = $.MulNum(dataPrice, datanum);
                        if (rowsum != dataSum) {
                            dataFlag = false;
                            return false;
                        }
                        //sums = sums + rowsum;
                        sums = $.SumNum(sums, rowsum);
                        AddData.push(row);
                    }

                });
                if (dataFlag == false) {
                    layer.msg("请输入正确数量", { icon: 2 });
                    return false;

                }
                $("#Sum").val(sums);

                var GiveSums = $("#GiveSum").val();

                if (AddData.length > 0) {
                    var Bill = {
                        "BillDate": BillDate,
                        "StroeInfo_id": StroeInfo_id,
                        "StoreName": StoreName,
                        "SellNameID": SellNameID,
                        "SellName": SellName,
                        "Sup_id": Sup_id,
                        "SupName": SupName,
                        "Sum": sums,
                        "GiveSum": GiveSums,
                        "BZ": BZ
                    };
                    var flag = false;
                    if (BillType == "add") {
                        $.loading(true, "正在提交数据");
                        $.ajax({
                            url: "/SellReturn/SaveBill",
                            data: { SHType: SHType, SellBills: Bill, SellBills_MX: AddData },
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (data) {
                                $.loading(false);
                                if (data.errorMsg) {
                                    flag = false;
                                    top.layer.msg(data.errorMsg, { icon: 2 });

                                }
                                else {
                                    var msgs = (data.okMsg);
                                    flag = true;
                                    top.layer.msg(msgs, { icon: 1 });

                                }
                            },
                            error: function () {
                                $.loading(false);


                            },
                            complete: function () {
                                $.loading(false);

                            }
                        });
                        return flag;
                    }
                    else {
                        $.loading(true, "正在提交数据");
                        $.ajax({
                            url: "/SellReturn/EditBill",
                            data: { ids: keyValueids, SHType: SHType, SellBills: Bill, SellBills_MX: AddData },
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (data) {
                                $.loading(false);
                                if (data.errorMsg) {
                                    flag = false;
                                    top.layer.msg(data.errorMsg, { icon: 2 });

                                }
                                else {
                                    var msgs = (data.okMsg);
                                    flag = true;
                                    top.layer.msg(msgs, { icon: 1 });

                                }
                            },
                            error: function () {
                                $.loading(false);


                            },
                            complete: function () {
                                $.loading(false);

                            }
                        });
                        return flag;

                    }
                }
                else {
                    layer.msg("请输入商品信息", { icon: 2 });
                    return false;
                }

            },
            //选择销售单
            Choose: function () {
                $.hands.Empty();
                var url = "/SellReturn/ChooseIndex";
                $.FromOpen('SellReturnFromChoose', '选择销售单', url, '680px', '570px', '1000');
            },
            init: function () {
                $('#Empty').on('click', $.hands.Empty);
                $('#SaveAdd').on('click', $.hands.SaveAdd);
                $('#AddClose').on('click', $.hands.AddClose);
                $('#AddSH').on('click', $.hands.AddSH);
                $('#Choose').on('click', $.hands.Choose);
            }
        }
        $(function () {
            $.hands.init();
        });
    });

    //计算和
    function SumMoney() {
        var sums = 0;
        var datas = $("#list1").jqGrid("getRowData");
        $.each(datas, function (i) {
            var row = datas[i];
            if (row.GoodName != "" && row.GoodName != undefined && row.GoodName != null) {
                var rowsum = Number(row.Sum);

                if (isNaN(rowsum) ) {
                    rowsum = 0;;
                }
                //sums = sums + rowsum;
                sums = $.SumNum(sums, rowsum);
            }

        });
        $("#Sum").val(sums);

    }
    //计算单价 数量 金额
    $(document).on('keyup', '#list1>tbody>tr>td>input[type="text"]', function () {
        var _inputName = $(this).attr('name');

        if (_inputName == "SellPrice") {
            var id = $("#list1").jqGrid('getGridParam', 'selrow');//根据点击行获得点击行的id（id为jsonReader: {id: "id" },）
            var rowData = $("#list1").jqGrid("getRowData", id);
            var _price = Number($(this).val());
            if (isNaN(_price)) {
                _price = 0;
            }
            var _num = Number(rowData.Num);
            //var _sum = (Math.round(_price * 100) * _num) / 100;
            var _sum = $.MulNum(_price, _num);
            $("#list1").jqGrid('setCell', id, 'Sum', _sum);
            SumMoney();

        } else if (_inputName == "Num") {
            var id = $("#list1").jqGrid('getGridParam', 'selrow');//根据点击行获得点击行的id（id为jsonReader: {id: "id" },）
            var rowData = $("#list1").jqGrid("getRowData", id);
            var _price = Number(rowData.SellPrice);
            var _num = Number($(this).val());
            if (isNaN(_num)) {
                _num = 0;
            }
            //var _sum = (Math.round(_price * 100) * _num) / 100;
            var _sum = $.MulNum(_price, _num);
            $("#list1").jqGrid('setCell', id, 'Sum', _sum);
            SumMoney();
        }
    });

</script>
<style>
    .form-control {
        height: 20px;
        text-align: center;
        display: table-cell;
        vertical-align: middle
    }
</style>







